package org.keyroy.mapEditor.ui.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.keyroy.map.core.animation.Animation;
import org.keyroy.map.core.manages.Resource;
import org.keyroy.map.core.sprite.Sprite;
import org.keyroy.mapEditor.ui.beans.UIUtil;
import org.keyroy.mapEditor.ui.views.AnimationView;
import org.keyroy.mapEditor.ui.views.ResourceView.OnSpritesSelectedListener;

public class AnimationSetDialogX extends Shell {
	private Text text;
	private Animation animation;
	private AnimationView animationView;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			AnimationSetDialogX shell = new AnimationSetDialogX(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public AnimationSetDialogX(Display display, final AnimationCreateListener listener) {
		super(display, SWT.DIALOG_TRIM);
		animation = new Animation();
		setLayout(new FormLayout());

		Composite composite_1 = new Composite(this, SWT.NONE);
		composite_1.setLayout(new FormLayout());
		FormData fd_composite_1 = new FormData();
		fd_composite_1.left = new FormAttachment(0, 10);
		fd_composite_1.right = new FormAttachment(100, -181);
		fd_composite_1.bottom = new FormAttachment(100, -11);
		composite_1.setLayoutData(fd_composite_1);

		ToolBar toolBar_1 = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		FormData fd_toolBar_1 = new FormData();
		fd_toolBar_1.bottom = new FormAttachment(composite_1, 0, SWT.BOTTOM);
		fd_toolBar_1.left = new FormAttachment(composite_1, 0, SWT.LEFT);
		toolBar_1.setLayoutData(fd_toolBar_1);

		ToolItem tltmNewItem_3 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UIUtil.open(new ResourceDialog(getDisplay(), new OnSpritesSelectedListener() {
					@Override
					public void onSpriteSelected(Resource resources, Sprite[][] sprites) {
						animation.add(sprites);
						animationView.reset(resources);
						ScrolledComposite scrolledComposite = (ScrolledComposite) animationView.getParent();
						scrolledComposite.setMinSize(animationView.computeSize(SWT.DEFAULT, SWT.DEFAULT));
						redraw();
					}
				}));
			}
		});
		tltmNewItem_3.setImage(SWTResourceManager.getImage(AnimationSetDialogX.class, "/res/icons/add_obj.gif"));
		tltmNewItem_3.setText("\u6DFB\u52A0");

		ToolItem tltmNewItem_4 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		tltmNewItem_4.setImage(SWTResourceManager.getImage(AnimationSetDialogX.class,
				"/res/icons/remove_correction.gif"));
		tltmNewItem_4.setText("\u5220\u9664");

		ToolItem tltmNewItem_5 = new ToolItem(toolBar_1, SWT.NONE);
		tltmNewItem_5.setImage(SWTResourceManager.getImage(AnimationSetDialogX.class, "/res/icons/debugt_obj.gif"));
		tltmNewItem_5.setText("\u8BBE\u7F6E");

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		fd_composite_1.top = new FormAttachment(0, 360);
		FormData fd_toolBar = new FormData();
		fd_toolBar.top = new FormAttachment(toolBar_1, 0, SWT.TOP);
		fd_toolBar.left = new FormAttachment(toolBar_1, 6);
		toolBar.setLayoutData(fd_toolBar);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setImage(SWTResourceManager.getImage(AnimationSetDialogX.class, "/res/icons/nav_go.gif"));
		tltmNewItem.setText("\u8FD0\u884C");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.setImage(SWTResourceManager.getImage(AnimationSetDialogX.class,
				"/res/icons/terminatedlaunch_obj.gif"));
		tltmNewItem_1.setText("\u505C\u6B62");

		Spinner spinner = new Spinner(this, SWT.BORDER);
		spinner.setMinimum(30);
		spinner.setSelection(40);
		FormData fd_spinner = new FormData();
		fd_spinner.top = new FormAttachment(0, 13);

		Group group = new Group(composite_1, SWT.NONE);
		FormData fd_group = new FormData();
		group.setLayoutData(fd_group);
		spinner.setLayoutData(fd_spinner);

		text = new Text(this, SWT.BORDER);
		FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(spinner, -6);
		text.setLayoutData(fd_text);
		fd_group.right = new FormAttachment(text, 0, SWT.RIGHT);
		fd_group.bottom = new FormAttachment(text, 282, SWT.BOTTOM);
		fd_group.top = new FormAttachment(text, 6);
		fd_group.left = new FormAttachment(text, 0, SWT.LEFT);

		Label lblNewLabel = new Label(this, SWT.NONE);
		fd_text.left = new FormAttachment(lblNewLabel, 6);
		fd_text.top = new FormAttachment(0, 13);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.bottom = new FormAttachment(toolBar_1, -287);
		fd_lblNewLabel.left = new FormAttachment(composite_1, 0, SWT.LEFT);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("\u540D\u79F0 : ");

		Canvas canvas = new Canvas(this, SWT.BORDER);
		FormData fd_canvas = new FormData();
		fd_canvas.bottom = new FormAttachment(toolBar, -5);
		fd_canvas.right = new FormAttachment(100, -10);
		fd_canvas.left = new FormAttachment(0, 181);
		canvas.setLayoutData(fd_canvas);

		Combo combo = new Combo(this, SWT.NONE);
		fd_canvas.top = new FormAttachment(0, 39);
		combo.setItems(new String[] { "\u4E2D\u5FC3", "\u5DE6\u4E0A", "\u5DE6\u4E2D", "\u5DE6\u53F3", "\u4E0B\u5DE6",
				"\u4E0B\u4E2D", "\u4E0B\u53F3" });
		FormData fd_combo = new FormData();
		fd_combo.top = new FormAttachment(0, 13);
		fd_combo.right = new FormAttachment(canvas, 0, SWT.RIGHT);
		combo.setLayoutData(fd_combo);
		combo.select(0);

		Button btnCheckButton = new Button(this, SWT.CHECK);
		fd_spinner.right = new FormAttachment(btnCheckButton, -6);
		btnCheckButton.setSelection(true);
		FormData fd_btnCheckButton = new FormData();
		fd_btnCheckButton.top = new FormAttachment(0, 16);
		btnCheckButton.setLayoutData(fd_btnCheckButton);
		btnCheckButton.setText("\u5FAA\u73AF");

		Label lblNewLabel_1 = new Label(this, SWT.CENTER);
		fd_btnCheckButton.right = new FormAttachment(lblNewLabel_1, -6);
		lblNewLabel_1.setAlignment(SWT.CENTER);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(0, 16);
		fd_lblNewLabel_1.right = new FormAttachment(combo, -6);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("\u5BF9\u9F50\u70B9 : ");

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				animation.name = text.getText();

				if (listener != null) {
					listener.onCreate(animation);
				}
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.right = new FormAttachment(canvas, 0, SWT.RIGHT);
		fd_btnNewButton.bottom = new FormAttachment(composite_1, 0, SWT.BOTTOM);
		fd_btnNewButton.left = new FormAttachment(spinner, 0, SWT.LEFT);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText(" \u5B8C\u6210 ");

		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(toolBar_1, -6);
		fd_composite.top = new FormAttachment(text, 6);
		fd_composite.left = new FormAttachment(composite_1, 0, SWT.LEFT);
		fd_composite.right = new FormAttachment(canvas, -6);

		ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		FormData fd_scrolledComposite = new FormData();
		fd_scrolledComposite.right = new FormAttachment(toolBar_1, 0, SWT.RIGHT);
		fd_scrolledComposite.bottom = new FormAttachment(toolBar_1, -6);
		fd_scrolledComposite.top = new FormAttachment(text, 6);
		fd_scrolledComposite.left = new FormAttachment(0, 10);
		scrolledComposite.setLayoutData(fd_scrolledComposite);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);

		animationView = new AnimationView(scrolledComposite);
		animationView.setAnimation(animation);
		scrolledComposite.setContent(animationView);
		scrolledComposite.setMinSize(animationView.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u52A8\u753B\u8BBE\u7F6E");
		setSize(633, 382);
	}

	public void resetAnimationView() {

	}

	@Override
	protected void checkSubclass() {
	}

	public static interface AnimationCreateListener {
		public void onCreate(Animation animation);
	}
}
